package com.uplooking.service.impl;

import java.io.StringReader;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import javax.transaction.Transactional.TxType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.uplooking.constant.AdminConstants;
import com.uplooking.constant.RedisConstants;
import com.uplooking.constant.UserConstants;
import com.uplooking.dao.RoleDO;
import com.uplooking.dao.UserDO;
import com.uplooking.pojo.DictVO;
import com.uplooking.pojo.RoleVO;
import com.uplooking.pojo.UserVO;
import com.uplooking.service.AdminService;

@Service("adminService")
public class AdminServiceImpl implements AdminService {

	@Autowired
	private UserDO userDO;
	
	@Autowired
	private RoleDO roleDO;
	
	@Autowired
	private StringRedisTemplate redis;
	
	@Override
	@Transactional(value=TxType.SUPPORTS)
	public Page<UserVO> getUserList(Map<String, Object> cond, int page, int size) {
		return userDO.findAll(new Specification<UserVO>() {
			@Override
			public Predicate toPredicate(Root<UserVO> rt, CriteriaQuery<?> cq, CriteriaBuilder cb) {
				Predicate predicate = cb.conjunction();
				if(cond.get(AdminConstants.CondRole)!=null 
						&& cond.get(AdminConstants.CondRole)!="") {
					predicate = cb.and(predicate,
							cb.equal(rt.get(AdminConstants.CondRole).as(String.class),
									cond.get(AdminConstants.CondRole)));
				}
				if(cond.get(AdminConstants.CondStatus)!=null) {
					predicate = cb.and(predicate,
							cb.equal(rt.get(AdminConstants.CondStatus).as(Integer.class),
									(int)cond.get(AdminConstants.CondStatus)));
				}
				return predicate;
			}
		}, PageRequest.of(page, size));
	}

	@Override
	@Transactional(value=TxType.SUPPORTS)
	public List<RoleVO> getRoleList() {
		return roleDO.findAll();
	}

	@Override
	public List<DictVO> getStatusList() {
		String text = redis.opsForHash().get(RedisConstants.PrefixDict, 
				RedisConstants.PrefixDictList+AdminConstants.DictUserStatus).toString();
		return JSONObject.parseArray(text, DictVO.class);
	}

	@Override
	@Transactional(value=TxType.REQUIRED)
	public Map<String, Object> checkShop(String username) {
		UserVO userVO = userDO.findById(username).orElse(null);
		if(userVO!=null) {
			userVO.setStatus(UserConstants.UserCommStatus);
			userDO.save(userVO);
			return AdminConstants.CheckShopSuccess();
		}else {
			return AdminConstants.ShopNotExists();
		}
	}

}
